/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.edu.itschapala.sistemas.septimo.vista;

import static com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type.Int;
import java.awt.Color;
import java.awt.event.KeyEvent;
import mx.edu.itschapala.sistemas.septimo.control.ClienteLnLocal;
import mx.edu.itschapala.sistemas.septimo.control.ProductoLnLocal;
import mx.edu.itschapala.sistemas.septimo.modelo.Cliente;
import mx.edu.itschapala.sistemas.septimo.modelo.Producto;
import mx.edu.itschapala.sistemas.septimo.vista.enumeracion.TipoVentana;

/**
 *
 * @author Estudiante
 */
public class ClienteCrearEditar extends javax.swing.JFrame {

    /**
     * Creates new form ProductoCrearEditar
     */
    //private ProductoLnLocal productoControl;
    private ClienteLnLocal clienteControl;
    private TipoVentana tipoVentana;
    private Cliente cliente;

    public ClienteCrearEditar(Cliente cliente,ClienteLnLocal clienteControl, TipoVentana tipoVentana) {
        this.clienteControl = clienteControl;
        this.tipoVentana = tipoVentana;
        this.cliente=cliente;
        initComponents();
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                jLabel6.setText("NUEVO CLIENTE");
                break;
            case VENTANA_MODIFICAR:
                jLabel6.setText("Modificar clienete");
                jTextFieldCodigo.setEditable(false);
                jTextFieldCodigo.setText(cliente.getId()+"");
                
                jTextFieldNombre.setText(cliente.getNombre());
                jTextFieldTelefono.setText(cliente.getTelefono()+"");
                jTextFieldDireccion.setText(cliente.getDireccion());
                jTextFieldSexo.setText(cliente.getSexo());
                break;
            case VENTANA_BUSQUEDA:
                jLabel6.setText("BUSCAR POR ID");
                jTextFieldNombre.setEditable(false);
                jTextFieldTelefono.setEditable(false);
                jTextFieldDireccion.setEditable(false);
                jTextFieldSexo.setEditable(false);
                break;
            case VENTANA_ELIMINAR:
                jLabel6.setText("Eliminar Cliente");
                jTextFieldNombre.setEditable(false);
                jTextFieldTelefono.setEditable(false);
                jTextFieldDireccion.setEditable(false);
                jTextFieldSexo.setEditable(false);
                break;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldTelefono = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldDireccion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldSexo = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        BotonAceptar = new javax.swing.JButton();
        BotonCancelar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridLayout(5, 2));

        jLabel1.setText("Codigo");
        jPanel1.add(jLabel1);

        jTextFieldCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldCodigoKeyPressed(evt);
            }
        });
        jPanel1.add(jTextFieldCodigo);

        jLabel2.setText("Nombre");
        jPanel1.add(jLabel2);
        jPanel1.add(jTextFieldNombre);

        jLabel3.setText("Telefono");
        jPanel1.add(jLabel3);
        jPanel1.add(jTextFieldTelefono);

        jLabel4.setText("Direccion");
        jPanel1.add(jLabel4);
        jPanel1.add(jTextFieldDireccion);

        jLabel5.setText("Sexo");
        jPanel1.add(jLabel5);
        jPanel1.add(jTextFieldSexo);

        jPanel2.add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.GridLayout(1, 2));

        BotonAceptar.setText("Aceptar");
        BotonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonAceptarActionPerformed(evt);
            }
        });
        jPanel3.add(BotonAceptar);

        BotonCancelar.setText("Cancelar");
        BotonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BotonCancelarActionPerformed(evt);
            }
        });
        jPanel3.add(BotonCancelar);

        jPanel2.add(jPanel3, java.awt.BorderLayout.PAGE_END);

        jLabel6.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 0, 51));
        jLabel6.setText("TITULO");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(81, 81, 81)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(149, 149, 149)
                        .addComponent(jLabel6)))
                .addContainerGap(60, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel6)
                .addGap(38, 38, 38)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(75, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BotonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_BotonCancelarActionPerformed

    private void BotonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BotonAceptarActionPerformed
        // TODO add your handling code here:
        switch (tipoVentana) {
            case VENTANA_REGISTRO:
                Cliente c = new Cliente();
                c.setId(Integer.parseInt(jTextFieldCodigo.getText()));
                c.setNombre(jTextFieldNombre.getText());
                c.setTelefono(Integer.parseInt(jTextFieldTelefono.getText()));
                c.setDireccion(jTextFieldDireccion.getText());
                c.setSexo(jTextFieldSexo.getText());
                clienteControl.registrar(c);
                jTextFieldCodigo.setText("");
                jTextFieldNombre.setText("");
                jTextFieldTelefono.setText("");
                jTextFieldDireccion.setText("");
                jTextFieldSexo.setText("");
                break;
            case VENTANA_BUSQUEDA:
                this.dispose();
                break;
            case VENTANA_MODIFICAR:
                c = new Cliente();
                c.setId(Integer.parseInt(jTextFieldCodigo.getText()));
                c.setNombre(jTextFieldNombre.getText());
                c.setTelefono(Integer.parseInt(jTextFieldTelefono.getText()));
                c.setDireccion(jTextFieldDireccion.getText());
                c.setSexo(jTextFieldSexo.getText());
                clienteControl.modificar(c);
                jTextFieldCodigo.setText("");
                jTextFieldNombre.setText("");
                jTextFieldTelefono.setText("");
                jTextFieldDireccion.setText("");
                jTextFieldSexo.setText("");
                jTextFieldNombre.setEditable(false);
                jTextFieldTelefono.setEditable(false);
                jTextFieldDireccion.setEditable(false);
                jTextFieldSexo.setEditable(false);
                break;
            case VENTANA_ELIMINAR:
                c = new Cliente();
                c.setId(Integer.parseInt(jTextFieldCodigo.getText()));
                clienteControl.eliminar(c);
                
        }


    }//GEN-LAST:event_BotonAceptarActionPerformed

    private void jTextFieldCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldCodigoKeyPressed
        // TODO add your handling code here:
        if(tipoVentana!=TipoVentana.VENTANA_REGISTRO){
            if(evt.getKeyCode()==KeyEvent.VK_ENTER){
                
         Cliente c= clienteControl.buscarPorCodigo(Integer.parseInt(jTextFieldCodigo.getText()));
         if(c!=null){
             jTextFieldNombre.setText(c.getNombre());
             jTextFieldNombre.setText(c.getTelefono()+"");
             jTextFieldNombre.setText(c.getDireccion());
             jTextFieldNombre.setText(c.getSexo());
             if(tipoVentana==TipoVentana.VENTANA_MODIFICAR){
                jTextFieldNombre.setEditable(true);
                jTextFieldTelefono.setEditable(true);
                jTextFieldDireccion.setEditable(true);
                jTextFieldSexo.setEditable(true);
             }
         }
                
            }
        }
    }//GEN-LAST:event_jTextFieldCodigoKeyPressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BotonAceptar;
    private javax.swing.JButton BotonCancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextFieldCodigo;
    private javax.swing.JTextField jTextFieldDireccion;
    private javax.swing.JTextField jTextFieldNombre;
    private javax.swing.JTextField jTextFieldSexo;
    private javax.swing.JTextField jTextFieldTelefono;
    // End of variables declaration//GEN-END:variables
}
